package com.zxl.yjb.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.bean.BannerBean;

import java.util.ArrayList;

public class BannerPagerAdapter extends PagerAdapter {
    private Context context;
    private ArrayList<BannerBean> datalist;
    private ItemClickListener itemClickListener;

    public BannerPagerAdapter(Context context, ArrayList<BannerBean> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener)
    {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener
    {
        void onitemClick(View view, int position);
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        view.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.item_banner_list,null);
        ImageView itemImageView = (ImageView) itemView.findViewById(R.id.iv_banner_bg);
        GlideApp.with(context).load(datalist.get(position).getImg()).into(itemImageView);
        container.addView(itemView);
        itemImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener!=null) {
                    itemClickListener.onitemClick(v,position);
                }
            }
        });
        return itemView;
    }
}
